package com.anan.ehcache.controller;

import com.anan.ehcache.entity.CacheEntity;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
 * @code @Author:Painter
 * @code @project_name:ehcache
 * @code @class_name:main
 * @code @system_login_name:23287
 * @code @time: 2023/5/30  14:20
 */

@Controller
@RestController
public class EhcacheController {


//    private  CacheManager cacheManager = CacheManager.create(System.getProperty("user.dir")+"/src/main/resources/ehcache.xml");

//    private CacheManager cacheManager = CacheManager.create();  // 配置文件默认（需要放在此目录）在/src/main/resources/ehcache.xml  这个目录下

    private static int count = 100;

    /**
     *
     * @param params  保存的对象  如果是java 自定义对象 例如 user() 对象  user对象需要实现Serializable 进行对象序列化
     * @return
     */
    @PostMapping("set")
    public String set(CacheEntity params) {
        CacheManager cacheManager = CacheManager.create();
        Cache cache = cacheManager.getCache("anan");  // 从 ehcache.xml 文件中获取 name=anan 的缓存
        Element element = new Element(params.getKey(), params);  // 创建一个缓存元素
        cache.put(element);  // 将元素保存在缓存中
//        cacheManager.shutdown();  // 关闭管理器
        return "SAVA OK";
    }


    @GetMapping("get")
    public CacheEntity get(String key) {
        CacheManager cacheManager = CacheManager.create();
        Cache cache = cacheManager.getCache("anan");
        Element element = cache.get(key);  // 根据key获取元素
        if (element == null) {
            return new CacheEntity(key);
        }
        CacheEntity cacheEntity = (CacheEntity) element.getObjectValue();  // 获取元素的对象值
//        cacheManager.shutdown();  // 关闭管理器
        return cacheEntity;
    }



    @GetMapping("delete")
    public String delete(String key){
        CacheManager cacheManager = CacheManager.create();
        Cache cache = cacheManager.getCache("anan");
        boolean remove = cache.remove(key);  // 根据key
//        cacheManager.shutdown();  // 关闭管理器
        if (remove) {
            return "DELETE OK";
        }
        return "DELETE FAIL";
    }

//    @GetMapping("/test")
//    public String test() {
//
//        String ceshi = getCeshi();
//        System.out.println(ceshi);
//        String ceshi2 = getCeshi();
//        System.out.println(ceshi2);
//        String s = deleteCeshi();
//        System.out.println(s);
//        String ceshi3 = getCeshi();
//        System.out.println(ceshi3);
//
//        return count+"";
//    }
//
//    @Cacheable(value = "anan",key="ceshi")
//    public String getCeshi(){
//        getcount();
//        return "100";
//    }
//
//    @CacheEvict(value = "anan",key="ceshi",allEntries = true)
//    public String deleteCeshi(){
//        return "删除数据";
//    }
//
//    public void getcount(){
//        System.out.println("获取数据");
//    }
}
